package com.liuym.admin.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.liuym.admin.repository.model.vo.AdminVO;
import com.liuym.admin.repository.model.vo.ClassScoreVO;
import com.liuym.admin.repository.model.vo.ClassScoreVO;
import com.liuym.admin.service.TableNameService;
import com.liuym.admin.util.ClassScoreTableNameUtil;
import com.liuym.admin.util.ClassScoreTableNameUtil;
import com.liuym.common.annotation.AdminLogin;
import com.liuym.common.annotation.AdminLoginUser;
import com.liuym.common.constants.Constants;
import com.liuym.common.constants.GradeResponse;
import com.liuym.common.dubbo.classes.api.ClassScoreDubboService;
import com.liuym.common.dubbo.classes.dto.ClassScoreDTO;
import com.liuym.common.dubbo.classes.dto.ClassDTO;
import com.liuym.common.dubbo.classes.dto.ClassScoreDTO;
import com.liuym.common.dubbo.classes.dto.ClassScoreDTO;
import com.liuym.common.exception.ClassException;
import com.liuym.common.exception.ClassException;
import com.liuym.common.exception.ClassException;
import com.vip.vjtools.vjkit.mapper.BeanMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author Liuym
 * @date 2018/12/10 13:39
 * @describe：
 */
@AdminLogin
@RestController
@RequestMapping("/admin/classScore")
public class ClassScoreController {

    private static Logger logger = LoggerFactory.getLogger(ClassScoreController.class);

    @Reference(version = "0.0.1")
    private ClassScoreDubboService classScoreDubboService;

    @Autowired
    private TableNameService tableNameService;



    @DeleteMapping("/deleteScore")
    public GradeResponse deleteScore(int id) {
        Integer deleteScore = null;
        try {
            ClassScoreDTO classScoreDTO = classScoreDubboService.getClassScoreById(tableNameService.findByTableAndTime(
                    Constants.CLASS_SCORE_TABLE_NAME).getTableName(), id);
            deleteScore = classScoreDTO.getFinallyScore();
            classScoreDubboService.deleteClassScore(tableNameService.findByTableAndTime(
                    Constants.CLASS_SCORE_TABLE_NAME).getTableName(), id);
        } catch (ClassException e) {
            logger.warn("eror", e);
            return GradeResponse.fail(e.getCode(), e.getMsg());
        }
        return GradeResponse.success(deleteScore);
    }

    @GetMapping("/getClassScore")
    public GradeResponse getClassScore(int id) {
        ClassScoreVO classScoreVO = new ClassScoreVO();
        try {
            ClassScoreDTO classScoreDTO = classScoreDubboService.getClassScoreById(tableNameService.findByTableAndTime(
                    Constants.CLASS_SCORE_TABLE_NAME).getTableName(), id);
            classScoreVO = BeanMapper.map(classScoreDTO, ClassScoreVO.class);
        } catch (ClassException e) {
            logger.warn("eror", e);
            return GradeResponse.fail(e.getCode(), e.getMsg());
        }
        return GradeResponse.success(classScoreVO);
    }
    @PostMapping("/updateClassScore")
    public GradeResponse updateClassScore(ClassScoreVO classScoreVO, @AdminLoginUser AdminVO adminVO) {
        ClassScoreDTO classScoreDTO = new ClassScoreDTO();
        if (classScoreVO != null) {
            classScoreDTO = BeanMapper.map(classScoreVO, ClassScoreDTO.class);
        }
        try {
            classScoreDTO.setOperator(adminVO.getUserName());
            classScoreDTO.setOperateDate(new Date());
            classScoreDubboService.updateClassScore(tableNameService.findByTableAndTime(
                    Constants.CLASS_SCORE_TABLE_NAME).getTableName(),
                    classScoreDTO,classScoreVO.getOldScore());
        } catch (ClassException e) {
            e.printStackTrace();
        }
        return GradeResponse.success();
    }

    @PostMapping("/addClassScore")
    public GradeResponse addClassScore(ClassScoreDTO classesScoreDTO, @AdminLoginUser AdminVO adminVO) {
        try {
            classScoreDubboService.addClassScore(
                    tableNameService.findByTableAndTime(
                            Constants.CLASS_SCORE_TABLE_NAME).getTableName(),
                    adminVO.getUserName(),
                    classesScoreDTO);
        } catch (ClassException e) {
            logger.warn("warn", e);
            return GradeResponse.fail(e.getCode(), e.getMsg());
        }
        return GradeResponse.success();
    }
}
